<?php
/*
 * @Author: 莫极通
 * @Phone: 13450159681
 * @Description: 
 * @Date: 2025-02-17 16:38:40
 * @LastEditTime: 2025-02-19 15:53:29
 */

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Feedback;
use app\model\Area;
use app\model\Geocoder;

class FeedbackController extends Base
{

    public function add()
    {

        $data = array_filter($this->getdata());
        $data['weid'] = weid();
        try {
            $res = Feedback::create($data);
        } catch (\Exception $e) {
            throw new ValidateException($e->getMessage());
        }
        return $this->json(['msg' => '添加成功', 'data' => $res->id]);
    }

    public function delete()
    {
        $idx =  $this->request->post('id', '', 'serach_in');
        if (!$idx) throw new ValidateException('参数错误');
        Feedback::destroy(['id' => $idx], true);
        return $this->json(['msg' => '操作成功']);
    }

    public function detail()
    {
        $id = input('get.id');
        if (!empty($id)) {
            $where['weid'] = weid();
            $where['uid'] = UID();
            $where['id'] = $id;
            $data = Feedback::where($where)->find();
            if (!empty($data)) {
                $data = $data->toArray();
                $data['image'] = json_decode($data['image'], true);
                $data['create_time'] = date('Y-m-d H:i:s', $data['create_time']);
            }
        }
        return $this->json(['data' => $data]);
    }

    public function list()
    {
        $where['weid'] = weid();
        $where['uid'] = UID();
        $data = Feedback::where($where)
            ->order('Id desc')
            ->select()
            ->toArray();
        foreach ($data as $key => &$val) {
            $val['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
        }
        unset($val);
        return $this->json(['data' => $data]);
    }
    public function getdata()
    {
        $postdata['weid'] = weid();
        $postdata['uid'] = UID();
        $postdata['message'] = input('post.message');
        $postdata['mobile'] = input('post.mobile');
        $postdata['email'] = input('post.email');

        $image = input('post.image');
        if (!empty($image)) {
            $postdata['image'] = json_encode(explode(',', $image));
        }
        $postdata['create_time'] = time();
        $postdata['status'] = 0;
        return $postdata;
    }
}
